import Local_Window_BattleSkillType from './windowSkillType.js'
let SWDA_battleSkill = {};

// 重定义 Window_BattleSkill
//-----------------------------------------------------------------------------
// Window_BattleSkill
//
// The window for selecting an item to use on the battle screen.
function Window_BattleSkill() {
    this.initialize.apply(this, arguments);
}

Window_BattleSkill.prototype = Object.create(Window_SkillList.prototype);
Window_BattleSkill.prototype.constructor = Window_BattleSkill;

Window_BattleSkill.prototype.initialize = function (x, y, width, height) {
    Window_SkillList.prototype.initialize.call(this, x, y, width, height);
    this._backgroundSprite = new Sprite();
    this._backgroundSprite.bitmap =
        ImageManager.loadBitmap('img/system/UI/', 'MenuBg_List');
    this.addChildAt(this._backgroundSprite, 0);
    this._backgroundSprite.x = -17;
    this._backgroundSprite.y = -1;

    this._actor = null;
    this.hide();
};

SWDA_battleSkill.Window_BattleSkill_isEnabled = Window_BattleSkill.prototype.isEnabled;
Window_BattleSkill.prototype.isEnabled = function(item) {
    let result = SWDA_battleSkill.Window_BattleSkill_isEnabled.call(this,item)
    if (this._actor)
        result = result && this._actor.canUse(item);
    return result;
    //return $gameParty.canUse(item);
};

Window_BattleSkill.prototype.setActor = function(actor) {
    if (this._actor !== actor) {
        this._actor = actor;
        this.refresh();
        this.resetScroll();
    }
};

Window_BattleSkill.prototype.show = function () {
    this.selectLast();
    this.showHelpWindow();
    Window_SkillList.prototype.show.call(this);
    BattleManager.requestElementSpritesUpdateAll();
};

Window_BattleSkill.prototype.hide = function () {
    this.hideHelpWindow();
    Window_SkillList.prototype.hide.call(this);
    BattleManager.requestElementSpritesUpdateAll();
};

Window_BattleSkill.prototype.maxCols = function () {
    return 1;
};


Window_BattleSkill.prototype.includes = function(item) {
    if (item.occasion > 1) return false;
    if (this._actor) {
      if (!this._actor.noHiddenSkillConditionsMet(item)) return false;
    }
    return Yanfly.Skill.Window_SkillList_includes.call(this, item);
};
//=============================================================================
// Scene_Battle
//=============================================================================


// 顶部分类菜单
Scene_Battle.prototype.createSkillTypeWindow = function () {
    this._skillTypeWindow = new Local_Window_BattleSkillType();
    this._skillTypeWindow.opacity = 0;
    // 分类窗口响应有问题
    //this._skillTypeWindow.setHandler('ok',     this.onSkillTypeOk.bind(this));
    this._skillTypeWindow.$on('select', this.onSkillTypeOk.bind(this));
    this._skillTypeWindow.hide();
    this._skillTypeWindow.deactivate();
    this.addWindow(this._skillTypeWindow);


    // 设置外观基础值
    const [x, y, width] = [650, 40, 500];
    this._skillTypeWindow.x = x
    this._skillTypeWindow.y = y
    this._skillTypeWindow.width = width

    // 关联行为
    //this._skillTypeWindow.filterRules = (item) => !['Weapons', 'Armors', 'Category:其他'].includes(item.trim())
    this._skillTypeWindow.refresh()
};


Scene_Battle.prototype.onSkillTypeOk = function () {
    if (this._skillWindow) {
        this._skillWindow.selectLast();
    }
    this._skillTypeWindow.refresh();
};

Scene_Battle.prototype.createSkillWindow = function () {
    this.createSkillTypeWindow();//在创建技能窗口前创建技能分类窗口
    const [x, y, width, height] = [680, 105, 312, 480];
    this._skillWindow = new Window_BattleSkill(x, y, width, height);
    this._skillWindow.setHelpWindow(this._helpWindow);
    this._skillWindow.width = 482
    this._skillWindow.opacity = 0
    this._skillWindow._scrollbar.setOffset(5)
    this._skillTypeWindow.setSkillWindow(this._skillWindow);
    this._skillWindow.setHandler('ok', this.onSkillOk.bind(this));
    this._skillWindow.setHandler('cancel', this.onSkillCancel.bind(this));
    this.addWindow(this._skillWindow);
};


// 选择技能时，隐藏所有技能相关窗口
SWDA_battleSkill.Scene_Battle_onSkillOk = Scene_Battle.prototype.onSkillOk;
Scene_Battle.prototype.onSkillOk = function () {
    SWDA_battleSkill.Scene_Battle_onSkillOk.call(this);
    this._skillTypeWindow.hide();
    this._skillTypeWindow.deactivate();
};

// 取消技能选择时，隐藏所有技能相关窗口
SWDA_battleSkill.Scene_Battle_onSkillCancel = Scene_Battle.prototype.onSkillCancel;
Scene_Battle.prototype.onSkillCancel = function () {
    SWDA_battleSkill.Scene_Battle_onSkillCancel.call(this);
    this._skillTypeWindow.hide();
    this._skillTypeWindow.deactivate();
};

// 取消目标选择时，恢复所有技能相关窗口
SWDA_battleSkill.Scene_Battle_onActorCancel = Scene_Battle.prototype.onActorCancel;
Scene_Battle.prototype.onActorCancel = function () {
    SWDA_battleSkill.Scene_Battle_onActorCancel.call(this);
    switch (this._actorCommandWindow.currentSymbol()) {
        case 'skill':
            this._skillTypeWindow.show();
            this._skillTypeWindow.activate();
            break;
    }
};

SWDA_battleSkill.Scene_Battle_onEnemyCancel = Scene_Battle.prototype.onEnemyCancel;
Scene_Battle.prototype.onEnemyCancel = function () {
    SWDA_battleSkill.Scene_Battle_onEnemyCancel.call(this);
    switch (this._actorCommandWindow.currentSymbol()) {
        case 'skill':
            this._skillTypeWindow.show();
            this._skillTypeWindow.activate();
            break;
    }
};

// 选择技能指令时，显示所有技能相关窗口
SWDA_battleSkill.SceneBattle_commandSkill = Scene_Battle.prototype.commandSkill;
Scene_Battle.prototype.commandSkill = function () {
    SWDA_battleSkill.SceneBattle_commandSkill.call(this);
    this._skillTypeWindow.show();
    this._skillTypeWindow.activate();
    this._skillTypeWindow.selectLast();
};

